/**
 * 
 */
package com.vision.core.sv.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.vision.core.cm.util.IDateFormat;


/**
 * Server-side implementation for {@link IDateFormat}. Uses {@link DateFormat}.
 * 
 * @author Mark
 *
 */
public class ServerDateFormat implements IDateFormat {

	private Map<String, DateFormat> cache = new HashMap<String, DateFormat>();
	
	@Override
	public String format(Date date, String format) {
		DateFormat df = cache.get(format);
		if (df == null)
			cache.put(format, df = new SimpleDateFormat(format));
		return df.format(date);
	}

	@Override
	public Date parse(String dateAsString, String format) {
		DateFormat df = cache.get(format);
		if (df == null)
			cache.put(format, df = new SimpleDateFormat(format));
		try {
			return df.parse(dateAsString);
		} catch (ParseException e) {
			throw new RuntimeException(e.getMessage());
		}
	}

}
